/* 
 * frm-index.js
 * @since 1.0 <2015-6-2> SoChishun Added.
 */
function frm_index_init() {

    // 导航菜单下拉设置
    if ('undefined' !== typeof ($.fn.superfish)) {
        $('ul.sf-menu').superfish({delay: 100});
    }
    if ('undefined' !== typeof ($.fn.marqueeLeft)) {
        $('.notices-panel').marqueeLeft({speed: 40, width: '500px', hide: true});
    }

    setInterval('clock()', 1000);
    setEggs();
    show_reload_tip();
    $('#need-reload').click(function () {
        reload_freepbx();
        return false;
    });

// 初始化历史导航菜单
    nav_history_bar.init();

    $('#main').load(function () {
        iframe_dialog.resize_iframe()
    });

    // 编辑项目按钮 2014-8-12 by sutroon
    // @example <a href="?s=XCall/Admin/admin_edit/id/[key].html" class="lnk-edit" data-dialog="{title:'编辑管理员',width:400,height:360}">编辑管理员</a>
    $('.lnk-edit').click(function () {
        var $this = $(this);
        var url = $this.attr('href');
        var settings = {title: $this.text(), width: 400, height: 420};
        var options = $this.data('dialog');
        if (options) {
            settings = $.extend(settings, eval('(' + options + ')'));
        }
        open_dialog(url, settings);
        return false;
    })

    // 关于 2014-8-14 by sutroon
    $('.lnk-about').click(function () {
        $('<div style="padding:20px; line-height:18px"><strong>XCall</strong><br />版本:3.0</div>').dialog({title: '关于'});
        return false;
    })
}

/**
 * 历史菜单导航条对象
 * @type object
 * @since 1.0 <2015-6-5> SoChishun Added.
 */
var nav_history_bar = {
    container: '#history-bar',
    'init': function () {
        // 动态加载记录
        nav_history_bar.load_nav_history();
        // 菜单事件委托
        $('#main-nav ul ul a').click(function () {
            nav_history_bar.add_nav_history($(this));
        })
        // 关闭事件委托
        $(nav_history_bar.container).delegate('i', 'click', function () {
            nav_history_bar.remove_nav_history($(this));
        })
        // 点击事件委托
        $(nav_history_bar.container).delegate('a', 'click', function () {
            $(this).parent().addClass('cur').siblings().removeClass('cur');
        })
    },
    /**
     * 添加到导航历史中
     * @since 1.0 2014 - 9 - 29 by sutroon
     * @since 2.0 < 2015 - 6 - 5 > SoChishun Refactoring
     */
    add_nav_history: function ($a) {
        var url = window.config.add_nav_history_url;
        $.get(url.replace('varkey', $a.data('id')), function (data) {
            if (data.status) {
                $(nav_history_bar.container + ' li').removeClass('cur');
                $(nav_history_bar.container + ' ul').append('<li class=" cur"><a href="' + $a.attr('href') + '" target="main">' + $a.text() + '</a><i class="ui-icon-close" data-id="' + $a.data('id') + '"></i></li>');
            } else {
                alert(data.info);
            }
        })
    },
    /**
     * 加载历史菜单
     * @since 1.0 2014-9-29 by sutroon
     * @since 2.0 <2015-6-5> SoChishun load_history()重构为 load_nav_history()
     */
    load_nav_history: function () {
        // [{"id":"3","name":"\u64cd\u4f5c\u5458\u7ba1\u7406","linkUrl":"\/index.php\/XCall\/Admin\/admin_list.html"}]
        $.post(window.config.load_nav_history_url.replace('varrnd', Math.random()), function (data) {
            if (data.status) {
                var n = data.info.length;
                var str = '';
                for (var i = 0; i < n; i++) {
                    str += '<li' + (data.cur == data.info[i].id ? ' class="cur"' : '') + '><a href="' + data.info[i].linkUrl + '" target="main">' + data.info[i].name + '</a><i class="ui-icon-close" data-id="' + data.info[i].id + '"></i></li>';
                }
                if (str) {
                    var url = $(nav_history_bar.container + ' li').first().removeClass('cur').parent().append(str).find('.cur a').attr('href');
                    $('#main').attr('src', "about:blank").attr('src', url);
                }
            }
        });
    },
    /**
     * 历史菜单按钮关闭事件
     * @since 1.0 2014-9-27 by sutroon
     * @since 2.0 <2015-6-5> SoChishun history_btn_close(id)重构为 remove_nav_history($elm)
     */
    remove_nav_history: function ($elm) {
        var url = window.config.remove_nav_history_url.replace('varkey', $elm.data('id'));
        $.get(url, function (data) {
            if (data.status) {
                var $cur = $elm.parent();
                var $ul = $cur.parent();
                var url = '';
                if ($cur.next()[0]) {
                    $cur.next().addClass('cur');
                    url = $cur.next().find('a').attr('href');
                }
                $cur.remove();
                if (!url) {
                    url = $ul.find('li:last').addClass('cur').find('a').attr('href');
                }
                $('#main').attr('src', "about:blank").attr('src', url);
            } else {
                alert(data.info);
            }
        });
    }
}

/**
 * 重载cti
 * @returns {undefined}
 */
function reload_freepbx() {
    $.ajax({
        type: "POST", url: '/admin/config.php', data: "handler=reload", dataType: "json", success: function (data) {
            if (data.status) {
                $('#need-reload').hide();
            } else {
                alert("重置失败!");
            }
        }
    });
}
function show_reload_tip() {
    $.get(window.config.cti_check_reload, function (data) {
        if (data.status == '1') {
            $('#need-reload').show();
        }
    });
}

/**
 * 时钟功能
 * @returns {void}
 * @since 1.0 2014-8-15 by sutroon
 */
function clock() {
    var date = new Date();
    var arrdate = [date.getMinutes(), date.getSeconds()];
    var text = (arrdate[0] < 10 ? '0' + arrdate[0] : arrdate[0]) + ':' + (arrdate[1] < 10 ? '0' + arrdate[1] : arrdate[1]);
    var h = date.getHours();
    var w = '';
    if (h < 5) {
        w = '凌晨了,注意身体,早点休息哦!';
    } else if (h >= 5 && h < 11) {
        w = '早上好,一日之计在于晨,加油哦^_^';
    } else if (h > 14 && h < 18) {
        w = '下午好,保持愉快心情努力工作哦~';
    } else if (h > 18 && h < 21) {
        w = '晚上好!';
    } else if (h >= 21 && h < 24) {
        w = '夜深了,还没休息啊!';
    }
    var week = ['日', '一', '二', '三', '四', '五', '六'];
    var title = (date.getMonth() + 1) + '月' + date.getDate() + '日 周' + week[date.getDay()] + ' ' + date.getHours() + ':' + arrdate[0] + '<br />' + w;
    $('#head-clock').attr('title', title).text(text);
}


/**
 * Eggs ^_^
 * @since 1.0 2014-9-27 by sutroon
 */
function setEggs() {
    var eggs_i = 0;
    $('.eggs').click(function () {
        eggs_i++;
        if (eggs_i >= 5) {
            location.href = '?s=Index/eggs';
        }
    })
}

// $.fn.marqueeLeft
// jqui-iframe-dialog